package com.shuoyue.ycgk.ui.mine.wrong;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.google.android.material.tabs.TabLayout;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.appcatch.SPUtils;
import com.shuoyue.ycgk.base.BaseMvpActivity;
import com.shuoyue.ycgk.base.BaseMvpFragment;
import com.shuoyue.ycgk.base.baseadapter.IndexFragmentStateAdapter;
import com.shuoyue.ycgk.entity.Type;
import com.shuoyue.ycgk.ui.common.CommonModuleContract;
import com.shuoyue.ycgk.ui.mine.collection.CollectionListActivity;
import com.shuoyue.ycgk.views.TabLayoutUtil;
import com.shuoyue.ycgk.views.viewpager_scroll.SuperViewPager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class WrongListActivity extends BaseMvpActivity<CommonModuleContract.Presenter>
        implements CommonModuleContract.View {

    @BindView(R.id.page_title)
    TextView pageTitle;
    @BindView(R.id.tab_layout)
    TabLayout tabLayout;
    @BindView(R.id.view_pager)
    SuperViewPager viewPager;
    @BindView(R.id.refreshlayout)
    SmartRefreshLayout refreshlayout;

    IndexFragmentStateAdapter indexFragmentAdapter;
    List<BaseMvpFragment> fragmentMainItems;

    int indexId = -1;//起点位置的ID

    public static void start(Context source, int position) {
        source.startActivity(new Intent(source, WrongListActivity.class)
                .putExtra("indexId", position));
    }


    @Override
    protected int getLayoutId() {
        return R.layout.activity_wrong_list;
    }

    @Override
    protected void initView() {
        pageTitle.setText("错题本");
        refreshlayout.setEnableLoadMore(false);
        refreshlayout.setOnRefreshListener(refreshLayout -> {
            mPresenter.getBaseModels(1);
            refreshLayout.finishRefresh(2000);
        });
    }

    @Override
    public void initData() {
        indexId = getIntent().getIntExtra("indexId", -1);
        mPresenter = new CommonModuleContract.Presenter();
        mPresenter.attachView(this);
        mPresenter.getBaseModels(1);
    }


    @OnClick(R.id.back)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void setBaseType(List<Type> baseTypes) {
        String selectBaseType = SPUtils.getLastSelectMoudle(mContext);
        for (Type item : baseTypes) {
            if (item.getName().equals(selectBaseType)) {
                mPresenter.getChildType(item.getId());
                break;
            }
        }
    }

    @Override
    public void setTabType(List<Type> type) {

        fragmentMainItems = new ArrayList<>();
        for (Type topType : type) {
            fragmentMainItems.add(FragmentWrongTab.getInstance(topType));
        }
        indexFragmentAdapter = new IndexFragmentStateAdapter(getSupportFragmentManager(), fragmentMainItems);
        tabLayout.setupWithViewPager(viewPager);
        viewPager.setOffscreenPageLimit(1);
        viewPager.setAdapter(indexFragmentAdapter);
        refreshlayout.setEnableRefresh(false);
        int index = 0;
        for (int i = 0; i < type.size(); i++) {
            if (indexId == type.get(i).getId()) {
                viewPager.setCurrentItem(i);
                index = i;
                break;
            }
        }
        TabLayoutUtil tabLayoutUtil = new TabLayoutUtil();
        tabLayoutUtil.setTabTextSizeWhenSelect(tabLayout, type,index);
    }
}
